package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.ui.UIUtils;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

//
// TODO: refactor this panel so that it can dinamically load
//       different content panels, also auto create toolbar etc.
//

/**
 * @author dimitri
 */
public class TaskActionsPanel extends JPanel {
    private static final long serialVersionUID = -1954356600524868626L;

    private ActionEditor editor;
    private Task task;

    public TaskActionsPanel() {
        setLayout(new BorderLayout(5, 5));
        setToolbarPage();
    }

    void setToolbarPage() {
        removeAll();
        JButton btnAddComment = new JButton();
        btnAddComment.setText("კომენტარის დამატება");
        btnAddComment.setIcon(UIUtils.getIcon16x16("email_add.png"));
        btnAddComment.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setContentPage();
            }
        });
        JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        toolbar.add(btnAddComment);
        add(toolbar, BorderLayout.SOUTH);
        resizeToFit();
    }

    void setContentPage() {
        removeAll();
        JButton btnSave = new JButton("გაგზავნა");
        JButton btnCancel = new JButton("გაუქმება");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (editor != null && editor.save()) {
                    editor.clear();
                    TaskActionsPanel.this.task = editor.getTask();
                    editor = null;
                    setToolbarPage();
                    fireTaskChange();
                }
            }
        });
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (editor != null) {
                    if (!editor.canClose()) {
                        return;
                    }
                    editor.clear();
                    editor = null;
                } 
                setToolbarPage();
            }
        });
        JPanel pnlBottom = new JPanel(new BorderLayout());
        pnlBottom.add(btnCancel, BorderLayout.WEST);
        pnlBottom.add(btnSave, BorderLayout.EAST);
        add(pnlBottom, BorderLayout.SOUTH);
        TaskCommentPane editorPane = new TaskCommentPane();
        editor = editorPane;
        editor.setTask(task);
        add(editorPane, BorderLayout.CENTER);
        editorPane.focusOnStart();
        getRootPane().setDefaultButton(btnSave);
        resizeToFit();
    }

    private void resizeToFit() {
        if (getParent() != null) {
            getParent().doLayout();
        }
        validate();
        doLayout();
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    protected void fireTaskChange() {
        firePropertyChange("task", "dummy old value", task);
    }

}
